"ui";
var intervals = [ "无间隔", "间隔1分钟", "间隔3分钟", "间隔5分钟",  "间隔10分钟", "间隔30分钟", "间隔60分钟"], mEntries = [];
var cycles = ["一直发送", "发送1次","发送5次", "发送10次", "发送15次", "发送20次", "发送30次", "发送50次"], mEntries1 = [];

ui.layout(
    <vertical w="*" h="*"  bg="#F0FFFF">
        <vertical marginRight="20" marginLeft="20" marginBottom="20" marginTop="75" w="*" h="auto" layout_gravity="center" gravity="center">
            <text id="toolTitle" line="1" textSize="24sp" textColor="black">旅游群发助手</text>
        </vertical>

        <vertical marginRight="20" marginLeft="20" marginBottom="20" w="*" h="auto">
            <text id="toolTitle" line="3">1、旅游群发助手,只是作为旅游同行工作中的推广辅助工具。</text>
            <text id="toolInfo" line="3">2、本工具完全免费使用,请勿相信从第三方购买。</text>
            <text id="toolInfo" line="3">3、关注公众号加入群聊一起交流吧。</text>
        </vertical>

        <horizontal marginLeft="20" paddingLeft="3" paddingTop="5" marginRight="20"  paddingBottom="5" w="*" h="150"  bg="#5479FC">
            <img scaleType="fitCenter" h="150" w="90"  src="file://sdcard/images/qq.png"/>
            <img scaleType="fitCenter" h="150" w="90"  src="file://sdcard/images/wx.png"/>
        </horizontal>

        <horizontal marginTop="10" marginLeft="20" paddingLeft="3" paddingTop="5" marginRight="20"  paddingBottom="5" w="*" h="50"  bg="#FF8C00">
            <spinner id="cycles" entries="{{cycles.join('|')}}" />
            <spinner id="intervals" entries="{{intervals.join('|')}}" />
        </horizontal>
        
        <horizontal marginTop="25" w="300" h="75" layout_gravity="center" gravity="center">
            <button style="Widget.AppCompat.Button.Colored" text="开始脚本" id="start"/>
            <button text="结束脚本" id="close"/>
        </horizontal>
    </vertical >
)

var selectCycle = 0;
var selectInterval = 1000 * 5;

ui.cycles.setOnItemSelectedListener(new android.widget.AdapterView.OnItemSelectedListener({
    onItemSelected: function (parent, view, position, id) {
      log('选中了第' + id + '项')
      switch(id){
            case 0:
                selectCycle = 0;
                break;
            case 1:
                selectCycle = 1;
                break;     
            case 2:
                selectCycle = 5;
                break;  
            case 3:
                selectCycle = 10;
                break;
            case 4:
                selectCycle = 15;
                break;
            case 5:
                selectCycle = 20;
                break;
            case 6:
                selectCycle = 30;
                break;  
            case 7:
                selectCycle = 50;
                break;
      }
    }
}));

ui.intervals.setOnItemSelectedListener(new android.widget.AdapterView.OnItemSelectedListener({
    onItemSelected: function (parent, view, position, id) {
      log('选中了第' + id + '项')
      switch(id){
            case 0:
                selectInterval = 1000 * 5;
                break;
            case 1:
                selectInterval = 1000 * 60;
                break;  
            case 2:
                selectInterval = 1000 * 180;
                break;
            case 3:
                selectInterval = 1000 * 300;
                break;
            case 4:
                selectInterval = 1000 * 600;
                break;
            case 5:
                selectInterval = 1000 * 1800;
                break;  
            case 6:
                selectInterval = 1000 * 3600;
                break;
      }
    }
}))

ui.start.click(function(){
    
    toast("开始执行脚本!");

    var grouPostEngine = engines.execScriptFile("sdcard/groupPost.js", {
        delay: 1000,
        loopTimes: selectCycle,
        interval: selectInterval
    });  
    
    var quickBtnEngine = engines.execScriptFile("sdcard/quickBtn.js");

    ui.close.click(function(params) {
        toast("关闭脚本！");
        engines.stopAll();
    });

});